package com.leigang.boot.controller;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RestController
public class ApiController {
    private static final Cache<String, Object> CACHE = Caffeine.newBuilder()
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .maximumSize(1000)
            .build();
    @PostConstruct
    public void init() {
        CACHE.put("hello", "world");
    }

    @GetMapping
    public String setCache(){
        CACHE.put("hello", "world111");
        return "设置缓存成功";
    }

    @GetMapping("get")
    public String getCache(){
        Object object = CACHE.getIfPresent("hello");
        return Objects.nonNull(object)?(String)object:"没有缓存值";
    }

}
